package com.tongji.housediy.view.titlewindows
{
	import com.tongji.housediy.events.ChangeStateEvent;
	import com.tongji.housediy.events.commonEvent.LoadAdminEvent;
	import com.tongji.housediy.events.designEvent.GetDesignsByUserEvent;
	import com.tongji.housediy.events.houseEvent.OpenHouseEvent;
	import com.tongji.housediy.model.DesignModel;
	import com.tongji.housediy.model.ExchangeDataModel;
	import com.tongji.housediy.model.GoodsModel;
	import com.tongji.housediy.model.HouseModel;
	import com.tongji.housediy.model.UserInforModel;
	import com.tongji.housediy.utils.LoadAdmin;
	import com.tongji.housediy.view.components.AbstractBorderChild;
	import com.tongji.housediy.view.components.AbstractDrawingRoom;
	import com.tongji.housediy.view.components.DoubleDoor;
	import com.tongji.housediy.view.components.DoubleShiftDoor;
	import com.tongji.housediy.view.components.DoubleWindow;
	import com.tongji.housediy.view.components.FixedWindow;
	import com.tongji.housediy.view.components.RoomBorder;
	import com.tongji.housediy.view.components.ShiftDoor;
	import com.tongji.housediy.view.components.SingleDoor;
	import com.tongji.housediy.vo.Goods;
	
	import de.polygonal.ds.DListNode;
	
	import flash.display.GraphicsBitmapFill;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.controls.Alert;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.events.IndexChangeEvent;
	
	/**
	 * 
	 * @author yanghanbing
	 * 2011/6/01
	 */ 
	public class OpenDesignWinMediator extends Mediator
	{
		[Inject]
		public var openDesignView:OpenDesignTitleWindow;
		
		[Inject]
		public var designModel:DesignModel;
		
		[Inject]
		public var goodsModel:GoodsModel;
		
		[Inject]
		public var roomModel:ExchangeDataModel;
		
		[Inject]
		public var userModel:UserInforModel;
		
		public var currentPage:int = 1;
		
		override public function onRegister():void
		{
			BindingUtils.bindProperty(openDesignView.designList.dataGroup, "dataProvider", designModel, "designArray");
			openDesignView.designList.addEventListener(IndexChangeEvent.CHANGE, editDesignHandler);
			openDesignView.closeBtn.addEventListener(MouseEvent.CLICK, closeWindowHandler);
			//分页相关
			//openDesignView.pageDivide.onePageBtn.addEventListener(MouseEvent.CLICK, getHouseByPage);
			openDesignView.nextPageBtn.addEventListener(MouseEvent.CLICK, getNextPage);
			openDesignView.lastPageBtn.addEventListener(MouseEvent.CLICK, getPreviousPage);
		}
		
		private function getPreviousPage(event:MouseEvent):void
		{
			if(currentPage == 1)
			{
				
			}else
			{				
				dispatch(new GetDesignsByUserEvent(GetDesignsByUserEvent.GET_DESIGNS_BY_USERID, currentPage - 1, 8, userModel.userID));
				currentPage = currentPage - 1;
			}			
		}
		
		private function getNextPage(event:MouseEvent):void
		{			
			dispatch(new GetDesignsByUserEvent(GetDesignsByUserEvent.GET_DESIGNS_BY_USERID, currentPage + 1, 8, userModel.userID));
			currentPage = currentPage + 1;
		}
		
		private function getHouseByPage(event:MouseEvent):void
		{
			event.target.label;
			Alert.show("ok");
		}
		
		private function closeWindowHandler(event:MouseEvent):void
		{
			PopUpManager.removePopUp(openDesignView);
			mediatorMap.removeMediatorByView(openDesignView);
		}
		
		private function editDesignHandler(event:IndexChangeEvent):void
		{
			PopUpManager.removePopUp(openDesignView);
			mediatorMap.removeMediatorByView(openDesignView);
			//设置当前打开的状态
			roomModel.isOpened = true;
			dispatch(new ChangeStateEvent(ChangeStateEvent.ROOM_OPENED));
			var roomCount:int = roomModel.gridContainer.numChildren;
			for(var length:int; length < roomCount; length++)
			{
				roomModel.gridContainer.removeChildAt(roomModel.gridContainer.numChildren - 1);
			}
			roomModel.openHouse(new XML(openDesignView.designList.selectedItem.xmlSrc));
		}
	}
}